<?php
/**
 * @package Post Publication Meta
 * @author Chris
 * @version 1
 */
/*
Plugin Name: Post Publication Meta
Plugin URI: http://chrissell.me
Description: Your mom.
Author: Chris
Version: 1
Author URI: http://chrissell.me
*/

 
define('MY_WORDPRESS_FOLDER',$_SERVER['DOCUMENT_ROOT']);
define('MY_THEME_FOLDER',str_replace("\\",'/',dirname(__FILE__)));
define('MY_THEME_PATH','/' . substr(MY_THEME_FOLDER,stripos(MY_THEME_FOLDER,'wp-content')));
 
add_action('admin_init','my_meta_init');
 
function my_meta_init()
{
	// review the function reference for parameter details
	// http://codex.wordpress.org/Function_Reference/wp_enqueue_script
	// http://codex.wordpress.org/Function_Reference/wp_enqueue_style
 
	//wp_enqueue_script('my_meta_js', MY_THEME_PATH . '/custom/meta.js', array('jquery'));
	wp_enqueue_style('my_meta_css', MY_THEME_PATH . '/meta.css');
 
	// review the function reference for parameter details
	// http://codex.wordpress.org/Function_Reference/add_meta_box
 
	// add a meta box for each of the wordpress page types: posts and pages
	foreach (array('post','page','news') as $type) 
	{
		add_meta_box('my_all_meta', 'Publishing Meta', 'my_meta_setup', $type, 'side', 'default');
	}
 
	// add a callback function to save any data a user enters in
	add_action('save_post','my_meta_save');
}
 
function my_meta_setup()
{
	global $post;
 
	// using an underscore, prevents the meta variable
	// from showing up in the custom fields section
	$meta['begin_date'] = get_post_meta($post->ID,'_publish_meta_begin_date',TRUE);
 	$meta['end_date'] = get_post_meta($post->ID,'_publish_meta_end_date',TRUE);
	$meta['publish_end'] = get_post_meta($post->ID,'_publish_meta_publish_end',TRUE);

	// instead of writing HTML here, lets do an include
	include(MY_THEME_FOLDER . '/meta.php');
 
	// create a custom nonce for submit verification later
	echo '<input type="hidden" name="my_meta_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
}
 
function my_meta_save($post_id) 
{
	// authentication checks
 
	// make sure data came from our meta box
	if (!wp_verify_nonce($_POST['my_meta_noncename'],__FILE__)) return $post_id;
 
	// check user permissions
	if ($_POST['post_type'] == 'page') 
	{
		if (!current_user_can('edit_page', $post_id)) return $post_id;
	}
	else 
	{
		if (!current_user_can('edit_post', $post_id)) return $post_id;
	}
 
	// authentication passed, save data
 
	// var types
	// single: _publish_meta[var]
	// array: _publish_meta[var][]
	// grouped array: _publish_meta[var_group][0][var_1], _publish_meta[var_group][0][var_2]
 
 
	$new_data = $_POST['_publish_meta'];
 	
	foreach($new_data as $key => $value){
		$meta_value = "_publish_meta_" . $key;
		
		$current_data = get_post_meta($post_id, $meta_value, TRUE);	
		my_meta_clean($new_data);
	 
	 
		if ($current_data) 
		{
			if (is_null($value)) delete_post_meta($post_id,$meta_value);
			else update_post_meta($post_id,$meta_value,$value);
		}
		elseif (!is_null($value))
		{
			add_post_meta($post_id,$meta_value,$value,TRUE);
		}
	}
 
	return $post_id;
}
 
function my_meta_clean(&$arr){
	if (is_array($arr)){
		foreach ($arr as $i => $v){
			if (is_array($arr[$i])) {
				my_meta_clean($arr[$i]);
 
				if (!count($arr[$i])) {
					unset($arr[$i]);
				}
			}else{
				if (trim($arr[$i]) == '') {
					unset($arr[$i]);
				}
			}
		}
 
		if (!count($arr)) {
			$arr = NULL;
		}
	}
}
 
?>